n = int(input())
circle = list(range(1,n+1)) # circle 内容为人的编号
number = 1 # number 代表正在报的数字
i = 0 # i 代表的是下标，0～n-1
while len(circle)>1 :
    if number % 7 == 0 or '7' in str(number) :
        del circle[i]
    else:
        i += 1
    if i == len(circle):
        i = 0
    number += 1
print(circle[0])

